package com.ruoyi.bussiness.service.impl;

import java.util.List;

import com.ruoyi.bussiness.domain.SysBusinessPortfolio;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bussiness.mapper.SysBusinessPortfolioMapper;
import com.ruoyi.bussiness.domain.SysBusinessPortfolio;
import com.ruoyi.bussiness.service.ISysBusinessPortfolioService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 作品集Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-23
 */
@Service
public class SysBusinessPortfolioServiceImpl implements ISysBusinessPortfolioService
{
    @Autowired
    private SysBusinessPortfolioMapper sysBusinessPortfolioMapper;

    /**
     * 查询作品集
     *
     * @param id 作品集主键
     * @return 作品集
     */
    @Override
    public SysBusinessPortfolio selectSysBusinessPortfolioById(Long id)
    {
        return sysBusinessPortfolioMapper.selectSysBusinessPortfolioById(id);
    }

    /**
     * 查询作品集列表
     *
     * @param sysBusinessPortfolio 作品集
     * @return 作品集
     */
    @Override
    public List<SysBusinessPortfolio> selectSysBusinessPortfolioList(SysBusinessPortfolio sysBusinessPortfolio)
    {
        return sysBusinessPortfolioMapper.selectSysBusinessPortfolioList(sysBusinessPortfolio);
    }

    /**
     * 新增作品集
     *
     * @param sysBusinessPortfolio 作品集
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysBusinessPortfolio(SysBusinessPortfolio sysBusinessPortfolio)
    {
        sysBusinessPortfolio.setCreateBy(SecurityUtils.getUsername());
        sysBusinessPortfolio.setCreateTime(DateUtils.getNowDate());
        sysBusinessPortfolio.setIsBottom("0");
        sysBusinessPortfolio.setIsTop("0");

        int i = sysBusinessPortfolioMapper.insertSysBusinessPortfolio(sysBusinessPortfolio);
        sysBusinessPortfolioMapper.updateOrderNum(sysBusinessPortfolio.getId());
        return i;
    }

    /**
     * 修改作品集
     *
     * @param sysBusinessPortfolio 作品集
     * @return 结果
     */
    @Override
    public int updateSysBusinessPortfolio(SysBusinessPortfolio sysBusinessPortfolio)
    {
        sysBusinessPortfolio.setCreateBy(SecurityUtils.getUsername());
        sysBusinessPortfolio.setUpdateTime(DateUtils.getNowDate());

        return sysBusinessPortfolioMapper.updateSysBusinessPortfolio(sysBusinessPortfolio);
    }

    /**
     * 批量删除作品集
     *
     * @param ids 需要删除的作品集主键
     * @return 结果
     */
    @Override
    public int deleteSysBusinessPortfolioByIds(Long[] ids)
    {
        return sysBusinessPortfolioMapper.deleteSysBusinessPortfolioByIds(ids);
    }

    /**
     * 删除作品集信息
     *
     * @param id 作品集主键
     * @return 结果
     */
    @Override
    public int deleteSysBusinessPortfolioById(Long id)
    {
        return sysBusinessPortfolioMapper.deleteSysBusinessPortfolioById(id);
    }

    @Override
    public AjaxResult moveUpOrDown(SysBusinessPortfolio entity) {
        SysBusinessPortfolio moveEntity = sysBusinessPortfolioMapper.selectSysBusinessPortfolioById(entity.getId());
        if(moveEntity == null){
            return AjaxResult.error("政策数据不存在，不能上移下移");
        }
        SysBusinessPortfolio moveChangeEntity = null;
        if("up".equalsIgnoreCase(entity.getMoveType())){
            moveChangeEntity = sysBusinessPortfolioMapper.getLastList(moveEntity.getOrderNum());
            if(moveChangeEntity == null){
                return AjaxResult.error("首行数据不能上移");
            }
            Integer orderNum = moveEntity.getOrderNum();
            moveEntity.setOrderNum(moveChangeEntity.getOrderNum());
            moveChangeEntity.setOrderNum(orderNum);
            sysBusinessPortfolioMapper.updateSysBusinessPortfolio(moveEntity);
            sysBusinessPortfolioMapper.updateSysBusinessPortfolio(moveChangeEntity);
        }else if("down".equalsIgnoreCase(entity.getMoveType())){
            moveChangeEntity = sysBusinessPortfolioMapper.getNextList(moveEntity.getOrderNum());
            if(moveChangeEntity == null){
                return AjaxResult.error("末行数据不能下移");
            }
            Integer orderNum = moveEntity.getOrderNum();
            moveEntity.setOrderNum(moveChangeEntity.getOrderNum());
            moveChangeEntity.setOrderNum(orderNum);
            sysBusinessPortfolioMapper.updateSysBusinessPortfolio(moveEntity);
            sysBusinessPortfolioMapper.updateSysBusinessPortfolio(moveChangeEntity);
        }else{
            return AjaxResult.error("无效的移动方式");
        }
        return AjaxResult.success("移动成功",1);
    }

    @Override
    public AjaxResult pinToTopOrEnd(SysBusinessPortfolio entity) {
        SysBusinessPortfolio moveEntity = sysBusinessPortfolioMapper.selectSysBusinessPortfolioById(entity.getId());
        if(moveEntity == null){
            return AjaxResult.error("政策不存在，不能上移下移");
        }
        if("top".equalsIgnoreCase(entity.getPinType())){
            if("1".equalsIgnoreCase(moveEntity.getIsTop())){
                moveEntity.setIsTop("0");
                moveEntity.setIsBottom("0");
            }else{
                moveEntity.setIsTop("1");
                moveEntity.setIsBottom("0");
            }
            sysBusinessPortfolioMapper.updateSysBusinessPortfolio(moveEntity);
        }else if("end".equalsIgnoreCase(entity.getPinType())){
            if("1".equalsIgnoreCase(moveEntity.getIsBottom())){
                moveEntity.setIsBottom("0");
                moveEntity.setIsTop("0");
            }else{
                moveEntity.setIsBottom("1");
                moveEntity.setIsTop("0");
            }
            sysBusinessPortfolioMapper.updateSysBusinessPortfolio(moveEntity);
        }else{
            return AjaxResult.error("无效的操作方式");
        }
        return AjaxResult.success("置顶置底成功",1);
    }
}
